/**
 * UIComponent.as
 * Showtime
 * version 1.0
 * 
 * Base class for all components.
 * 
 * Copyright (c) 2011 Keith Peters
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package showtime.framework.ui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	public class UIComponent extends Sprite
	{
		private var _enabled:Boolean  = false;		
		private var _data:Object = null;
		
		protected var _width:Number = 0;		
		protected var _height:Number = 0;
		
		public function UIComponent(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0)
		{			
			initialize();
			
			move(xpos, ypos);	
			parent != null && parent.addChild(this);
		}
		
		public static function initializeStage(stage:Stage):void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}	
		
		protected function initialize():void
		{			
			createChildren();
			
			invalidate();
		}
		
		protected function createChildren():void
		{
			
		}
		
		protected function invalidate():void
		{
			addEventListener(Event.ENTER_FRAME, validateNow);
		}
		
		protected function validateNow(evt:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, validateNow);
			updateDisplayList();
		}
		
		public function updateDisplayList():void
		{
			
		}
	
		public function move(xpos:Number, ypos:Number):void
		{
			x = Math.round(xpos);
			y = Math.round(ypos);
		}
		
		public function size(width:Number, height:Number):void
		{
			_width = width;
			_height = height;
			invalidate();
			
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		public function removeAllChilds():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		}
		
		public function set enabled(value:Boolean):void
		{
			mouseEnabled = mouseChildren = value;
			tabEnabled = tabChildren = value;		
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		
		public function getData():Object
		{
			return _data;
		}
		
		public function setData(value:Object):void
		{
			_data = value;
		}
	}
}